from django import template
from django.utils.html import format_html
# from UsersViews import index
register = template.Library()

@register.filter
def kong_upper(val):
    return val.upper()
# 自定义标签
@register.simple_tag
def jia(a,b):
    res = int(a)+int(b)
    return res

@register.simple_tag
def cheng(a,b):
    res = a*b
    return res

# # 自定义 分页标签
@register.simple_tag
def showpage(num,request):
    # 获取当前页码数
    p = int(request.GET.get('page',1))
    start = p-3
    end = p+1

    # 判断 当前页如果小于3
    if p <= 3:
        start=1
        end = 5

    if p > num-2:
        start = num-5
        end = num

    if num <= 6:
        start = 1
        end = num
    s = ''
    s += '<li><a href="?page=1">首页</a></li>'
    if p-1 <= 1:
        s += '<li class="disabled"><a href="?page=1">上一页</a></li>'
    else:
        s += f'<li><a href="?page={p-1}">上一页</a></li>'
    for x in range(start,end+1):
        if x == p:
            s += f'<li class="active"><a href="?page={x}">{x}</a></li>'
        else:
            s += f'<li><a href="?page={x}">{x}</a></li>'
    if p+1 >= num:
        s += f'<li class="disabled"><a href="?page={num}">下一页</a></li>'
    else:
        s += f'<li><a href="?page={p+1}">下一页</a></li>'
    s += f'<li><a href="?page={num}">尾页</a></li>'
    return format_html(s)
